package com.ucenter.server.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.system.comm.model.Page;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;
import com.ucenter.server.user.dao.UserWebsiteDao;
import com.ucenter.server.user.pojo.UserWebsite;
import com.ucenter.server.user.service.UserWebsiteService;
import com.ucenter.server.user.service.WebsiteService;

/**
 * user_website的Service
 * @author autoCode
 * @date 2018-11-29 15:15:25
 * @version V1.0.0
 */
@Component
public class UserWebsiteServiceImpl implements UserWebsiteService {

	@Autowired
	private UserWebsiteDao userWebsiteDao;
	@Autowired
	private WebsiteService websiteService;
	
	@Override
	public ResponseFrame save(UserWebsite userWebsite) {
		ResponseFrame frame = new ResponseFrame();
		UserWebsite org = get(userWebsite.getUserId(), userWebsite.getWebsiteId());
		if(org == null) {
			userWebsiteDao.save(userWebsite);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public UserWebsite get(String userId, String websiteId) {
		return userWebsiteDao.get(userId, websiteId);
	}

	@Override
	public ResponseFrame pageQuery(UserWebsite userWebsite) {
		userWebsite.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = userWebsiteDao.findUserWebsiteCount(userWebsite);
		List<UserWebsite> data = null;
		if(total > 0) {
			data = userWebsiteDao.findUserWebsite(userWebsite);
		}
		Page<UserWebsite> page = new Page<UserWebsite>(userWebsite.getPage(), userWebsite.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String userId, String websiteId) {
		ResponseFrame frame = new ResponseFrame();
		userWebsiteDao.delete(userId, websiteId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<UserWebsite> findByUserId(String userId) {
		List<UserWebsite> list = userWebsiteDao.findByUserId(userId);
		for (UserWebsite uw : list) {
			uw.setWebsiteName(websiteService.getName(uw.getWebsiteId()));
		}
		return list;
	}

	@Override
	public void deleteByUserId(String userId) {
		userWebsiteDao.deleteByUserId(userId);
	}
}